﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;

namespace TuongLaiDangCho.Enterface
{
    public partial class ucTimeTable : DevExpress.XtraEditors.XtraUserControl
    {
        LenLichDTO timeTable =new LenLichDTO();
        public ucTimeTable()
        {
            InitializeComponent();
            //timeTable = new LenLichDTO();
            timeTable.NgayThang = frmHome.choiseDate;
            groTimeTable.Text=DataProvider.Today(frmHome.choiseDate) + " " + frmHome.choiseDate.ToShortDateString();
        }
        void LoadTimeTable()
        {
            grcTimeTable.DataSource = null;
            grcTimeTable.DataSource = LenLichDAO.LoadTimeTable(timeTable);
        }

        private void ucTimeTable_Load(object sender, EventArgs e)
        {
            LoadTimeTable();
        }

        private void grv_TimeTable_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void grv_TimeTable_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string s_Error="";
            bool b_Vali=true;
            if (grv_TimeTable.GetRowCellValue(e.RowHandle, "Gio").ToString() == "" || grv_TimeTable.GetRowCellValue(e.RowHandle, "NoiDung").ToString() == "")
            {
                b_Vali = false;
                s_Error = "Không thể để trống mục này";
                LoadTimeTable();
            }
            if (b_Vali)
            {
                if (e.RowHandle < 0)
                {
                    timeTable.NoiDung = grv_TimeTable.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    timeTable.Gio = grv_TimeTable.GetRowCellValue(e.RowHandle, "Gio").ToString();
                    //XtraMessageBox.Show(timeTable.Gio + "\n" + timeTable.NoiDung, "them moi");
                    LenLichDAO.InsertTimeTable(timeTable);
                    LoadTimeTable();
                }
                else
                {
                    timeTable.NoiDung = grv_TimeTable.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    timeTable.ID = int.Parse(grv_TimeTable.GetRowCellValue(e.RowHandle, "ID").ToString());
                    timeTable.Gio = grv_TimeTable.GetRowCellValue(e.RowHandle, "Gio").ToString();
                    //XtraMessageBox.Show(timeTable.Gio + "\n" + timeTable.NoiDung +"\n"+timeTable.ID.ToString(), "Update");
                    LenLichDAO.UpdateTimeTable(timeTable);
                }
            }
            else
            {
                b_Vali = false;
                XtraMessageBox.Show(s_Error);
            }

        }

        private void grcTimeTable_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && grv_TimeTable.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Are You Sure?", "Quets", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                   timeTable.ID=int.Parse(grv_TimeTable.GetRowCellValue(grv_TimeTable.FocusedRowHandle,"ID").ToString());
                   LenLichDAO.DeleteTimeTable(timeTable);
                   LoadTimeTable();
                }
            }
        }
    }
}
